package com.supostacks.infrastructure.repository;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supostacks.domain.repository.IGetRecordsRepository;
import com.supostacks.domain.vo.GetRecordsVo;
import com.supostacks.infrastructure.context.DefaultContext;
import com.supostacks.infrastructure.context.EntityUtil;
import com.supostacks.infrastructure.dao.GetRecordsDao;
import com.supostacks.infrastructure.po.GetRecords;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class GetRecordsRepository extends DefaultContext implements IGetRecordsRepository {

    @Resource
    private GetRecordsDao getRecordsDao;

    @Override
    public String insert(GetRecordsVo getRecordsVo) {
        try {
            GetRecords getRecords = new GetRecords();
            EntityUtil.copyFields(getRecordsVo, getRecords);

            int insert = getRecordsDao.insert(getRecords);
            if (insert > 0) {
                return getRecords.getId();
            }
            return null;

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(String id) {
        getRecordsDao.deleteById(id);
    }

    @Override
    public String updateById(GetRecordsVo getRecordsVo) {
        try {
            GetRecords getRecords = new GetRecords();
            EntityUtil.copyFields(getRecordsVo, getRecords);
            int i = getRecordsDao.updateById(getRecords);
            if (i > 0) {
                return getRecords.getId();
            }
            return "";
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public GetRecordsVo selectById(String id) {
        try {
            GetRecords getRecords = getRecordsDao.selectById(id);
            if (getRecords != null) {
                GetRecordsVo getRecordsVo = new GetRecordsVo();
                EntityUtil.copyFields(getRecords, GetRecordsVo.class);
                return getRecordsVo;
            }
            return null;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<GetRecordsVo> selectList(JSONObject param) {
        QueryWrapper<GetRecords> queryWrapper = where(param);
        List<GetRecords> getRecordsInfos = getRecordsDao.selectList(queryWrapper);
        return buildList(getRecordsInfos);
    }

    @Override
    public JSONObject selectByPage(JSONObject options, int pageNo, int pageSize) {
        return null;
    }

    public List<GetRecordsVo> buildList(List<GetRecords> getRecordsInfos) {
        return getRecordsInfos.stream().map(getRecordsInfo -> {
            try {
                GetRecordsVo getRecordsInfoVo = new GetRecordsVo();
                EntityUtil.copyFields(getRecordsInfo, getRecordsInfoVo);
                return getRecordsInfoVo;
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
    }
}
